<?php

namespace App\Http\Controllers\Article;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function GuzzleHttp\Promise\all;

class ArticleController extends Controller
{
    public  function  index()
    {
        //查询文章所有数据
        $allData = Article::all()->toArray();

        //渲染视图
        return view('articleshow.show',compact('allData'));
    }
    //点赞
    public  function love(Request $request)
    {
        //接收前台传来的id
       $id =  $request->get('id');

        //根据id查询要点赞的一条记录
       $loveInfo = Article::find($id);

       if($loveInfo['love'] == 0){
           DB::table('article')->where(['id'=>$id])->increment('love');
           return ['code'=>200,'data'=>[]];
       }else{
           return ['code'=>500,'data'=>[]];
       }
    }
    //详情页展示
    public  function show($id)
    {
        //查询一条详情信息
        $commentData = Comment::all()->toArray();


        $oneData = Article::find($id);
        return view('one.one',compact('oneData','commentData'));
    }
    //添加评论页面
    public  function commentAdd()
    {
        return view('comment.add');
    }
    //添加评论
    public  function  addComment(Request $request)
    {
        //表单验证
            $request->validate([
               'main'=>'required'
            ]);
           $addData =  $request->except(['_token']);

           $addRes  = Comment::create($addData);
           if ($addRes){
               return redirect(route('index'));
           }

    }
    //展示评论列表
    public  function  commentShow()
    {
        //查询所有评论
        $allData = Comment::paginate(3);

        return view('comment.list',compact('allData'));
    }
}
